package com.turing.post.salesmanage.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.salesmanage.util.Sales;
import com.turing.post.salesmanage.util.SalesDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Dec 20, 2012
 */
@Stateless(mappedName = "salesmanage.impl.SalesDaoImpl")
@Remote
@Local
public class SalesDaoImpl implements SalesDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@SuppressWarnings("unchecked")
	@Override
	public Sales getSalesBySalesName(String salesName) {
		String jpql = "select * from Sales c where c.salesname =?1";
		Query query = _em.createNativeQuery(jpql, Sales.class).setParameter(1,
				salesName);
		List<Sales> list = query.getResultList();
		if (list == null || list.isEmpty() || list.size() < 1) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public Sales getSalesBySalesNo(BigInteger salesId) {
		return _em.find(Sales.class, salesId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Sales> getSalesList(PCPSWebInput webInput, Integer page,
			String branchNo, String salesName) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from Sales t where SalesName like ?1";
		sql += PCPSUtil.getSqlCond(webInput, branchNo, "SaleBranchNo");
		sql += " order by salesId";
		Query query = _em.createNativeQuery(sql, Sales.class).setFirstResult(
				startPosition).setMaxResults(maxResult);
		if (PCPSUtil.isEmpty(salesName)) {
			salesName = "";
		}
		query.setParameter(1, "%" + salesName + "%");
		return query.getResultList();
	}

	@Override
	public BigInteger getSalesTotal(PCPSWebInput webInput, String branchNo,
			String salesName) {
		String sql = "select nvl(count(*),0) from Sales t where SalesName like ?1";
		sql += PCPSUtil.getSqlCond(webInput, branchNo, "SaleBranchNo");
		Query query = _em.createNativeQuery(sql);
		if (PCPSUtil.isEmpty(salesName)) {
			salesName = "";
		}
		query.setParameter(1, "%" + salesName + "%");
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertSales(Sales sales) throws PCPSApplicationException {
		GeneralDao.insert(_em, sales);
	}

	@Override
	public void updateSales(Sales sales) throws PCPSApplicationException {
		GeneralDao.update(_em, sales);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Sales> getSalesListBySaleBranchNo(String saleBranchNo) {
		String sql = "select * from Sales t where saleBranchNo = ?1";
		Query query = _em.createNativeQuery(sql, Sales.class);
		query.setParameter(1, saleBranchNo);
		return query.getResultList();
	}
}
